package com.plugin.uniapp.utils;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;

import com.plugin.uniapp.dialog.MultiViewDialog;

public class Logger {
    private static Handler handler = new Handler(Looper.getMainLooper());

    public static void log(String msg, Class clazz, boolean logToScreen) {
        Log.i("[B]" + clazz.getSimpleName(), msg);
        if (logToScreen) {
            logToScreen("[" + clazz.getSimpleName() + "]" + msg);
        }
    }

    public static void error(String msg, Class clazz, boolean logToScreen) {
        Log.e("[B]" + clazz.getSimpleName(), msg);
        if (logToScreen) {
            logToScreen("[" + clazz.getSimpleName() + "]" + msg);
        }
    }

    private static void logToScreen(String msg) {
        handler.post(() -> {
            if (MultiViewDialog.instance != null && MultiViewDialog.instance.txtLogger != null) {
                TextView logger = MultiViewDialog.instance.txtLogger;
                logger.append(msg + "\n");
            }
        });
    }
}
